<?php
namespace Common\Model;
use Think\Model;

class LogModel extends Model{

    /**
     * 添加日志
     */
    public function _edit($uid,$log){
        if(!$uid || $log == ''){
            return 0;//不操作 
        }
        $data = [
            'uid' => $uid,
            'create_time' => time(),
            'log' => $log
        ];

        return $this->add($data); //执行添加操作
    }

    public function _query($p = 1, $row = 10, $query = ''){
        $sql = 'SELECT A.*,B.realname,B.avatar FROM mini_log A left join mini_users B on A.uid = B.id WHERE 1=1 ';
        $sqlcount = 'SELECT COUNT(DISTINCT A.`id`) AS tp_count FROM mini_log A left join mini_users B on A.uid = B.id WHERE 1=1 ';
        if ($query) {
            $sql .= $query;
            $sqlcount .= $query;
        }
        //order
        $total = $this->query($sqlcount);
        $data['page']['total'] = $total[0]['tp_count'];
        //处理删除最后一条数据的时候没有数据的bug,请求最后一页没有数据的bug
        if(floor(($data['page']['total'] + $row - 1) / $row) < $p && $p != 1){
            $p = $p - 1;
        }
        $sql .= ' ORDER BY A.id desc ';
        $sql .= ' LIMIT '.($p - 1) * $row.','.$row;
        $data['data'] = $this->query($sql);
       
        $data['page']['cur'] = $p;
        $data['page']['row'] = $row;
        // $data['sql'] = $sql;
        return $data;
    }


}